package com.zzyl.nursing.service.impl;

import java.util.List;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author zwd
 * @date 2025-01-02
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id) {
        //1）根据planId查询NursingPlan
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);

        //2）将NursingPlan的属性值拷贝到NursingPlanVo
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        //3）根据planId查询NursingProjectPlanVo  项目计划和项目中间表的值
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.selectByPlanId(nursingPlan.getId());

        //4）将查询的NursingProjectPlan集合赋值给NursingPlanVo.projectPlans
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //1）将计划数据写入nursing_plan
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //2）将项目列表写入nursing_project_plan   insert into nursing_project_plan(xx)values(?,?)
        int count = nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(), nursingPlan.getId());
        return count;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto dto) {
        //1）将Dto转成NursingPlan
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);

        //2）删除当前护理计划关联的项目列表
        nursingProjectPlanMapper.deleteByPlanId(dto.getId());

        //3）判断护理计划关联的项目列表是否为空，如果不为空，则批量添加到nursing_project_plan
        if (dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0) {
            nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), dto.getId());
        }

        //4）修改护理计划
        nursingPlan.setUpdateTime(DateUtils.getNowDate());
        return nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids) {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id) {
        //删除护理计划关联的项目列表【中间表 nursiong_project_plan】
        nursingProjectPlanMapper.deleteByPlanId(id);

        //删除护理计划
        return nursingPlanMapper.deleteNursingPlanById(id);
    }
}
